"use strict";
const electron = require("electron");
const path$1 = require("path");
const path = require("path");
require("fs");
const si = require("systeminformation");
path.join(__dirname, "logs", "app.log");
const { exec } = require("child_process");
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
process.env.LANG = "zh_CN.UTF-8";
process.env.LANGUAGE = "zh_CN:zh";
electron.autoUpdater.autoDownload = false;
const createWindow = async () => {
  const win = new electron.BrowserWindow({
    width: 1400,
    height: 900,
    frame: true,
    //隐藏头部
    resizable: false,
    //用户是否可以调整窗口大小
    show: true,
    //先不显示窗口，等窗口准备好了才显示，防止渲染未完成时出现白框
    maximizable: false,
    //禁止双击最大化
    webPreferences: {
      nodeIntegration: true,
      // 设置是否在页面中启用 Node.js 集成模式
      contextIsolation: false,
      // 设置是否启用上下文隔离模式。
      certificateTransparency: true
      // 其他的webPreferences配置
    }
  });
  win.webContents.openDevTools();
  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
  } else {
    console.log(444, path$1.join(__dirname));
    const indexHtml = path$1.join(__dirname, "/../dist/index.html");
    win.loadFile(indexHtml);
  }
  const initLoginWindow = (windowObj) => {
    windowObj.setTitle("登录");
    windowObj.setSize(1400, 900);
    windowObj.setMinimumSize(1400, 900);
    windowObj.setResizable(false);
    windowObj.setMaximizable(false);
    windowObj.center();
  };
  const initMainWindow = (windowObj) => {
    windowObj.setTitle("首页");
    windowObj.setSize(1400, 900);
    windowObj.setMinimumSize(1400, 900);
    windowObj.setResizable(true);
    windowObj.setMaximizable(true);
    windowObj.center();
  };
  const delayShowWindow = (initFn, delay) => {
    win.setOpacity(0);
    initFn(win);
    win.minimize();
    setTimeout(() => {
      win.setOpacity(1);
      win.show();
      win.focus();
    }, delay);
  };
  win.on("ready-to-show", () => {
    win.show();
  });
  electron.ipcMain.on("window-close", () => {
    win.close();
  });
  electron.ipcMain.on("window-max", function() {
    if (win.isMaximized()) {
      win.restore();
    } else {
      win.maximize();
    }
  });
  electron.ipcMain.on("window-min", function() {
    win.minimize();
  });
  electron.ipcMain.on("showMainWindow", (event, delay = 500) => {
    if (delay) {
      delayShowWindow(initMainWindow, delay);
    } else {
      initMainWindow(win);
    }
  });
  electron.ipcMain.on("log-loaded", (event, arg) => {
    console.log(arg, "log-loaded");
  });
  electron.ipcMain.on("showLoginWindow", (event, delay = 500) => {
    console.log(delay);
    if (delay) {
      delayShowWindow(initLoginWindow, delay);
    } else {
      initLoginWindow(win);
    }
  });
  delayShowWindow(initMainWindow, 100);
  handleFileOpen();
  path.join(__dirname, "../logs", "app.log");
  electron.ipcMain.on("message-from-renderer", (event, arg) => {
    event.reply("message-to-renderer", arg);
  });
  const zhuban = await getZhuBan();
  const bios = await getBios();
  const cpu = await getCpu();
  const str = zhuban + bios + cpu + "gugechanggong";
  const encoder = new TextEncoder();
  const bytes = encoder.encode(str);
  const crypto = require("crypto");
  const str1 = crypto.createHash("md5").update(bytes).digest("hex").toUpperCase();
  const pcid = str1.substr(8, 16);
  electron.ipcMain.on("message-from-renderer-pcid", (event, arg) => {
    event.reply("message-to-renderer-pcid", pcid);
  });
};
async function handleFileOpen() {
}
function getZhuBan() {
  return new Promise((resolve, reject) => {
    si.baseboard().then((data) => {
      resolve(data.serial);
    }).catch((error) => console.error(error));
  });
}
function getBios() {
  return new Promise((resolve, reject) => {
    exec("wmic bios get serialnumber", (error, stdout, stderr) => {
      if (error) {
        console.error(`执行的错误: ${error}`);
        return;
      }
      if (stderr) {
        console.error(`stderr: ${stderr}`);
        return;
      }
      resolve(stdout.split("\r\r\n")[1].trim());
    });
  });
}
function getCpu() {
  return new Promise((resolve, reject) => {
    exec("wmic cpu get ProcessorId", (error, stdout, stderr) => {
      if (error) {
        console.error(`执行的错误: ${error}`);
        return;
      }
      resolve(stdout.split("\r\r\n")[1].trim());
    });
  });
}
electron.autoUpdater.on("error", (error) => {
  console.error(error);
});
electron.autoUpdater.on("update-available", async () => {
  console.log("更新可用，正在下载...");
  const { version } = info;
  const { response, checkboxChecked } = await electron.dialog.showMessageBox({
    type: "info",
    buttons: ["关闭", "跳过这个版本", "安装更新"],
    title: "软件更新提醒",
    message: `最新版本是 ${version}，您现在的版本是 ${electron.app.getVersion()}，现在要下载更新吗？`,
    defaultId: 2,
    cancelId: -1,
    checkboxLabel: "以后自动下载并安装更新",
    checkboxChecked: false,
    textWidth: 300
  });
  if ([1, 2].includes(response)) {
    let updaterData = {
      version,
      skip: response === 1,
      auto: checkboxChecked
    };
    setLocalData({
      updater: {
        ...updaterData
      }
    });
    if (response === 2)
      electron.autoUpdater.downloadUpdate();
  }
});
electron.autoUpdater.on("update-not-available", () => {
  console.log("没有发现新的更新");
});
electron.autoUpdater.on("update-downloaded", () => {
  console.log("更新已下载，准备安装");
});
electron.app.whenReady().then(() => {
  createWindow();
});
